"""
实现下载吧的用户登录功能；
1、键盘输入用户名和密码
2、判断用户名和密码是否正确
3、输入验证结果
"""
import pymysql
import hashlib

def connect_db():
    con = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        port=3306,
        database='db_xzb'
    )
    if con != None:
        return con
    else:
        return None


def login(username, password):
    """
    用户登录功能
    :param username:
    :param password:
    :return: True/False
    """

    con = connect_db()
    cursor = con.cursor()
    sql = 'select * from tb_user where username = %s and password = %s'
    result = cursor.execute(sql, (username, password))
    print("result", result)

    if result != None and result >= 1:
        return True
    else:
        return False


if __name__ == '__main__':
    username = input("输入姓名：")
    password = input("输入密码：")
    password = hashlib.md5(password.encode("utf_8")).hexdigest()
    print(password)
    result = login(username, password)
    if result:
        print("登录成功")
    else:
        print("登录失败")
